/*
 * ChessServer.java
 * 
 * Created on 16.9.2007, 21:49:26
 * 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package cz.possoft.server.main;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

import cz.possoft.generic.utils.GenericUtils;
import cz.possoft.server.threads.ChessServerSocket;
import cz.possoft.server.threads.PlayerChecker;

/**
 * Hlavni trida serveru. Implementuje ServerSocket (posluchac)
 * ke kteremu se mohou pripojit klienti (hraci).
 * 
 * @author mara
 */
public class ChessServer {
        
  // Maximalni pocet klientu (vlaken), kteri muzou byt spojeni. Zatim nefunguje, neomezeno.
  public static final int MAXPLAYERS=100;
  // Flag udava jestli se ma vypnout server po killnuti vsech klientu
  public static final boolean KILL_SERVER_WHEN_ALL_PLAYERS_DISCONNECTED = false;

  // flag ziska hodnotu true v momente, kdy se nekdo pripoji
  private static boolean somebodyConnectFlag = false;
  
  public static void main(String[] args) throws IOException {
  
	  ServerSocket ss = new ServerSocket(GenericUtils.PORT);
  
	  // socket na serveru
	  Socket s;
  
	  // toto vlakno prohlizi pole hracu po 5 sekundach a zasila vsem hracum info o tom, kdo je null atd. 
	  PlayerChecker playerChecker;
  
	  try {
		  playerChecker = new PlayerChecker();
		  playerChecker.start();  
    
		  while (true) {
			  	System.out.println("Server posloucha: " + ss);
			  	s = ss.accept();
			  	ChessServerSocket st = new ChessServerSocket(s);
			  	somebodyConnectFlag = true;     
          }
    
      }
	  finally {
		  System.out.println("Konci Server ss: " + ss);
		  ss.close();
      }  
  }

  public static boolean isSomebodyConnectFlag() {
        return somebodyConnectFlag;
  }

                  
}
